<?php
class Upload_model extends CI_Model
{
    var $gallery_path;
    var $gallery_path_url;
    function Upload_model ()
    {
        parent::__construct();
        $this->gallery_path = realpath(APPPATH . '../my_upload');
        $this->gallery_path_url = base_url() . 'my_upload/';
    }
    function generate_code ($length = 10)
    {
        if ($length <= 0) {
            return false;
        }
        $code = "";
        $chars = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
        srand((double) microtime() * 1000000);
        for ($i = 0; $i < $length; $i ++) {
            $code = $code . substr($chars, rand() % strlen($chars), 1);
        }
        return $code;
    }
    function do_upload ()
    {
        //Gen Random code for new file name
        $config = array('allowed_types' => 'jpg|jpeg|gif|png', 
        'upload_path' => $this->gallery_path, 'max_size' => 2000);
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $image_data = $this->upload->data();
        $randomcode = $this->generate_code(12);
        $file = $image_data['file_name'];
        $newimagename = $randomcode . $image_data['file_ext'];
        rename($image_data['full_path'], 
        $image_data['file_path'] . $newimagename);
        /* @formatter:on */
        $config = array(
        'source_image' => $image_data['file_path'] . $newimagename,  //$image_data['full_path'],
        'new_image' => $this->gallery_path . '/thumbs2', 
        'maintain_ration' => true, 'width' => 243, 'height' => 162);
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();

        
        $config = array(
        'source_image' => $image_data['file_path'] . $newimagename,  //$image_data['full_path'],
        'new_image' => $this->gallery_path . '/thumbs3', 
        'maintain_ration' => true, 'width' => 510, 'height' => 340);
        //$this->load->library('image_lib', $config);
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        
        $config = array(
        'source_image' => $image_data['file_path'] . $newimagename,  //$image_data['full_path'],
        'new_image' => $this->gallery_path . '/thumbs', 
        'maintain_ration' => true, 'width' => 143, 'height' => 95);
        //$this->load->library('image_lib', $config);
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        
        echo $this->image_lib->display_errors();
        
        return array_merge($image_data,array('new_name'=>$newimagename));
    }
    function get_images ()
    {
        $files = scandir($this->gallery_path);
        $files = array_diff($files, array('.', '..', 'thumbs'));
        $images = array();
        foreach ($files as $file) {
            $images[] = array('url' => $this->gallery_path_url . $file, 
            'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file);
        }
        return $images;
    }
}



